package cn.xiangxu.telecom.cost.dao;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.sql.DataSource;

import org.springframework.stereotype.Repository;

import cn.xiangxu.telecom.cost.entity.CostInfo;

@Repository("costDao")
public class CostDaoJdbcImpl implements Serializable, CostDao {
	@Resource(name="dbcp")
	private DataSource ds;
	public List<CostInfo> findAllCost() {
		Connection conn = null;
		try {
			conn = ds.getConnection();
			String sql = "SELECT * "
					+ "FROM cost ";
			Statement st = conn.createStatement();
			ResultSet rs = st.executeQuery(sql);
			List<CostInfo> list = new ArrayList<CostInfo>();
			while(rs.next()) {
				CostInfo ci  = new CostInfo();
				ci.setCost_id(rs.getInt("cost_id"));
				ci.setName(rs.getString("name"));
				ci.setBase_duration(rs.getDouble("base_duration"));
				ci.setUnit_cost(rs.getDouble("unit_cost"));
				ci.setStatus(rs.getString("status"));
				ci.setCreatime(rs.getTimestamp("creatime"));
				ci.setStartime(rs.getTimestamp("startime"));
				list.add(ci);
			}
			return list;
			
		} catch(SQLException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		} finally {
			if(null != conn) {
				try {
					conn.close();
				} catch(SQLException e) {
					e.printStackTrace();
					throw new RuntimeException(e);
				}
			}
		}
	}

}
